class Spoj1Controller < ApplicationController
  layout 'standart'
  include Configurator
  #include Authorizer
  #before_filter :login_required

  def index
    @is_admin = admin?(session[:user])
  end

  def show_log
    run = Run.find_by_run_id(params[:run])
    if !params[:run] || !run
      flash[:error] = "No run selected"
      redirect_to :action => :index
    else
      @log = run.log
    end       
  end
  
  def do_judge 
    add_judge_thread
  end

  def view_board
    config = load_config
    unless params[:contest] 
      flash[:error] = "No contest selected!"
      redirect_to :action => :index
    else
      @contest = Contest.find_by_contest_id(params[:contest])
      unless @contest
        flash[:error] = "The selected contest does not exist"
        redirect_to :action => :index
      end
    end
    @problems = Problem.find(:all,:conditions => {:contest_id => @contest})
    @users = User.find(:all,:conditions => {:id => 
          Run.find(:all,:select => "DISTINCT user_id",:conditions => {:problem_id => @problems}).map{|u| u.user_id}})
    @results = {}
    @users.each do |u|
      @results[u.name] = {:all => 0,:attempts => 0,:time => 0}
      @problems.each do |p|
        o = Run.find(:first,:order => :submit_time,:conditions =>["user_id = ? AND problem_id = ? AND status = ? AND
            submit_time >= ?",u,p,config['status']['ok'],@contest.start_time])
        if o
          success_time = ((o.submit_time - @contest.start_time)/60).to_i
          unsuccessfull = Run.find(:all,:conditions =>["user_id = ? AND problem_id = ? 
          AND status IN(?) AND submit_time <= ? AND submit_time >= ? ",u,p,wrong_status,o.submit_time,@contest.start_time])
          @results[u.name][p.problem_id] = ok_status_string(unsuccessfull.size,success_time)
          @results[u.name][:all] += 1
          @results[u.name][:attempts] += unsuccessfull.size+1
          @results[u.name][:time] += success_time + unsuccessfull.size*config['wrong_submit_penalty']
        else
          unsuccessfull = Run.find(:all,:order => "submit_time DESC",:conditions => {:user_id => u,:problem_id => p,:status =>
                wrong_status})
          wa_time = 0
          if unsuccessfull.size > 0
            wa_time =  ((unsuccessfull[0].submit_time - @contest.start_time)/60).to_i
          end          
          @results[u.name][p.problem_id] = wrong_status_string(unsuccessfull.size,wa_time)
          @results[u.name][:attempts] += unsuccessfull.size
        end
      end
    end
    @results = @results.sort_by{|k,v| [-v[:all],v[:time]]}
  end
  def status
    config = load_config
    h = {}
    if params[:contest] && params[:problem].nil?
      @contest = Contest.find_by_contest_id(params[:contest])
      problems = Problem.find(:all, :conditions => {:contest_id => @contest.contest_id})
      h[:problem_id] = problems.map{|p| p.problem_id}
    end
    unless params[:user].nil?
      @user = User.find_by_id(params[:user])
      h[:user_id] = params[:user] 
    end
    unless params[:problem].nil?
      @problem = Problem.find_by_problem_id(params[:problem])
      h[:problem_id] = @problem.problem_id
    end
    order_string = ""
    
    @order = params[:order]
    ord = params[:order]
    ord = ord[1,ord.length] if ord && ord[0,1] == '-'
    if @order && config['runs_can_be_sorted_by'].include?(ord)
      order_string = config['runs_can_be_sorted_by'][ord]
      order_string += " DESC " if params[:order][0,1] == '-'
    end
    @runs = ""
    if order_string.length > 0
      @runs = Run.paginate(:page => params[:page], :conditions => h,:include => ['problem','potrebitel'],
        :order => order_string,:per_page => config['number_per_page'])
    else
      @runs = Run.paginate(:page => params[:page], :conditions => h,:include => ['problem','potrebitel'],
        :per_page => config['number_per_page'])
    end
  end
  def download_tests
    problem = Problem.find_by_problem_id params[:problem]
    if problem.tests
      send_file(problem.tests, :disposition => 'attachment') 
    else
      @problem = problem
      flash[:error] = "Could not download tests"
      render :action => :edit_problem
    end
  end
  
  def download_answers
    problem = Problem.find_by_problem_id params[:problem]
    if problem.answers
      send_file(problem.answers, :disposition => 'attachment') 
    else
      @problem = problem
      flash[:error] = "Could not download answers"
      render :action => :edit_problem
    end
  end

  def make_submit
    session[:problem_wanted] = params[:problem]
    redirect_to new_run_path
  end
end
